package leetcode_bin_tree_test;

import bin_tree.TreeNode;

/**
 * 938. 二叉搜索树的范围和
 */
public class RangeSumBST {
    int ret;
    public int rangeSumBST(TreeNode root, int low, int high) {
        if (root == null) {
            return 0;
        }
        rangeSumBST(root.left, low, high);
        if (root.val >= low && root.val <= high) {
            ret += root.val;
        }
        // 剪枝
        if (root.val > high) {
            return ret;
        }
        rangeSumBST(root.right, low, high);
        return ret;
    }
}
